<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css"/>
    <link rel="stylesheet" type="text/css" href="../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../css/mui.min.css"/>
    <style>
        body {
            font-size: 14px;
        }

        .mui-numbox {
            height: 25px;
            padding: 0 23px;
            width: 80px;
        }

        .mui-numbox [class*=btn-numbox], .mui-numbox [class*=numbox-btn] {
            width: 23px;
        }

        .aui-list-view i.aui-iconfont {
            font-size: 20px;
        }

        .aui-counter-list .aui-checkbox, .aui-counter-list .aui-radio {
            margin-top: 18px;
            width: 19px;
            height: 19px;
        }

        .aui-checkbox:checked:before, .aui-checkbox.aui-checked:before {
            top: -1px;
            left: 1px;
        }

        .aui-checkbox:checked:after, .aui-checkbox.aui-checked:after {
            content: "";
        }

        .aui-checkbox {
            margin: 12px 0;
            width: 19px;
            height: 19px;
        }

        .tips {
            font-size: 12px;
            margin-top: 7px;
            height: 22px;
            line-height: 22px;
            width: 22px;
        }

        .aui-list-view-cell p {
            line-height: 16px;
        }
    </style>
</head>
<body class="">
<div class="cars">
    <div class="carList">
        <!--<div class="comName aui-padded-10 bgWhite aui-border-b"><i class="aui-iconfont aui-icon-shop"></i>供货商:广州公司</div>-->
        <div class="aui-content">
            <div class="kongshuju aui-text-center">
            <p>购物车暂无商品</p>

            <div class="goBuy" tapmode onclick="goBuy()">
                <div><img src="../image/goshopping.png" style="width:80px;height:80px;" /></div>
                <div style="font-size:16px;text-align: center">去购物?</div>
            </div>
            <!-- <p class="goBuy" tapmode onclick="goBuy()"><span>去购物?</span></p> -->
        </div>
            <ul class="aui-list-view after-no-border" id="carList-content">

            </ul>
        </div>
    </div>
</div>

<footer class="aui-nav aui-padded-0-10" id="aui-footer">
    <ul class="aui-bar-tab aui-flex-col aui-padded-5-0  ">
        <div class="aui-flex-item-3"><input class="aui-checkbox aui-margin-r-5 allChoose " type="checkbox">
            <div class="aui-flex-row aui-flex-middle" style="height: 45px;">全选</div>
        </div>
        <div class="aui-flex-item-5 aui-text-right aui-padded-r-5">
            <p class="active font-weight-700">总计：￥<em class="totalMoney">---</em>元</p>
            <p class="font-black-12">不含运费</p>
        </div>
        <div class="aui-flex-item-4" tapmomde onclick="goToCarOrder()">
            <div class="">
                <div class="aui-btn  bg-active aui-btn-block aui-padded-10 colorWhite ">
                    去结算
                </div>
            </div>
        </div>
    </ul>
</footer>

<script id="carList-template" type="text/x-dot-template">
    {{ for (var i in it) { }}
    <li class="aui-list-view-cell aui-img aui-counter-list">
        <!--<input class="aui-radio aui-margin-r-5" type="radio" name="demo1" checked>-->
        <input value="{{=it[i].goods_id}}" class="aui-checkbox aui-margin-r-5 singleCheck" type="checkbox">
        <img class="aui-img-object aui-pull-left aui-margin-r-5" src="{{=it[i].goods_info.original_img}}">
        <div class="aui-img-body aui-flex-col">
            <div class="aui-flex-item-7  ">
                <p class="aui-ellipsis-1 font-size-12">{{=it[i].goods_name}}</p>
                {{? it[i].goods_info.vmoney_user>0}}
                <input type="hidden" value="{{=it[i].goods_info.vmoney_user}}" class="my-song-jifen">

                <p class="aui-ellipsis-1 font-size-12 aui-text-danger">赠送<em class="emSongJifen">{{=it[i].goods_info.vmoney_user*it[i].goods_number}}</em>积分</p>
                {{??}}
                <p class="aui-ellipsis-1 font-size-12 aui-text-danger">
                    <input type="hidden" value="{{=it[i].goods_info.vmoney_unbackable}}"  class="my-di-jifen">
                    <em class="emDiJifen1"> {{=it[i].goods_info.vmoney_unbackable*it[i].goods_number}}</em>积分抵<em class="emDiJifen2">{{=it[i].goods_info.vmoney_unbackable*it[i].goods_number}}</em>元</p>

                {{?}}
                <div class="mui-numbox aui-margin-t-5" data-numbox-min='1' data-numbox-max='100'>
                    <button class="mui-btn mui-numbox-btn-minus" type="button">-</button>
                    <input class="mui-numbox-input" value="{{=it[i].goods_number}}" type="number"/>
                    <button class="mui-btn mui-numbox-btn-plus" type="button">+</button>
                </div>
            </div>
            <div class="aui-flex-item-5 aui-text-right">
                <p class="  text-line-through">¥ {{=it[i].market_price}}元</p>
                <p class=" aui-text-danger  ">¥ <em class="price">{{=it[i].goods_info.show_price}}</em>元</p>
                <p class=" aui-text-danger  aui-padded-t-5"><i data-id="{{=it[i].rec_id}}"
                                                 class="aui-iconfont  aui-text-default aui-icon-delete delet"
                ></i></p>
            </div>
        </div>
    </li>
    {{}}}
</script>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/myInfo.js"></script>
<script type="text/javascript" src="../script/jquery-1.11.3.js"></script>
<script type="text/javascript" src="../script/mui.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript">
    var imgBase64, isLock = false;
    window.postType = 1;
    var myUid;
    var myName;
    var page = 1;
    var rong;
    var latitude, longitude;
    var emotionData;
    apiready = function () {
        api.parseTapmode();
        // getInfo();

        //发布完成后
        api.addEventListener({
            name: 'publishPostDone'
        }, function (ret, err) {
            headerChange(2, true);
        });

        //下拉刷新
//        api.setRefreshHeaderInfo({
//            visible: true,
//            loadingImg: 'widget://image/ptr_pull.png',
//            bgColor: '#efeff4',
//            textColor: '#959595',
//            textDown: '下拉刷新',
//            textUp: '松开刷新',
//            showTime: true
//        }, function (ret, err) {
//            page = 1;
//            isLock = false;
////            loadData();
//        });

        // 监听是否达到底部上拉加载
//        api.addEventListener({
//            name: 'scrolltobottom'
//        }, function (ret, err) {
//            page++;
//            if (!isLock)  getCarList();
//        });
        getStorageAll();
        if(user_id){
            getCarList();
        }

        api.addEventListener({
            name: "updateCar"
        }, function (ret, err) {
            if (ret && ret.value) {
                getCarList();
            }
        })

    }
    /**
     * add by moqin
     * 没有商品时候去购物
     */
    function goBuy() {
        api.execScript({
            name: "root",
            script: 'randomSwitchBtnBakHome(0)'
        })
    }
    //    选择所有
    $(".allChoose").click(function () {
        var $AllThis = $(this)
        $(".cars").find("input[type='checkbox']").each(function () {
            $(this).prop("checked", $AllThis.prop("checked"))
        })
        showTotal()
    })
    $(document).on("click", ".singleCheck", function () {
        var len = $(".cars").find("input[type='checkbox']").length;
        var checkedNum = 0;
        $(".cars").find("input[type='checkbox']").each(function () {
            if ($(this).prop("checked")) {
                checkedNum++;
            }
        })
        if (len == checkedNum) {
            $(".allChoose").prop("checked", true)
        } else {
            $(".allChoose").prop("checked", false)
        }
        showTotal();
    })
    $(document).on("change", ".mui-numbox-input", function () {
        var count=$(this).val();
        var song_jifen=$(this).parent().parent().find(".my-song-jifen").eq(0).val();
        var di_jifen=$(this).parent().parent().find(".my-di-jifen").eq(0).val();
        $(this).parent().parent().find(".emDiJifen1").eq(0).text((count*di_jifen).toFixed(2));
        $(this).parent().parent().find(".emDiJifen2").eq(0).text((count*di_jifen).toFixed(2));
        $(this).parent().parent().find(".emSongJifen").eq(0).text((count*song_jifen).toFixed(2));
        showTotal();
    })
    $(document).on("click", ".delet", function () {
        var goodId = $(this).attr("data-id");

        if (confirm("确定删除？")) {
            $(this).closest("li").hide("slow", function () {
//            执行删除数据库
                $(this).closest("li").remove();
                var url = "cart/delete";
                var data = {};
                data["rec_ids"] = goodId;
                getStorageAll();
                ajaxRequest(url, 'post', data, function (ret, err) {
                    api.toast({
                        msg: "删除成功"
                    })
                });
                getCarList();
                showTotal()

            });
        }

    })
    function goToCarOrder() {
        var $chooseInput = $(".cars").find("input[type='checkbox']:checked");
        if ($chooseInput.length == 0) {
            api.toast({
                msg: "请选择要购买的商品",
                location: "middle"
            })
            return false;
        }
        var num = "";
        var goodId = "";
        var goodJson = "["
        $(".cars").find("input[type='checkbox']:checked").each(function (index) {
            if (index > 0) {
                goodJson += ",";
            }
            num = $(this).parent().find(".mui-numbox-input").eq(0).val();
            goodId = $(this).val();
            goodJson += '{"goods_id":' + goodId + ",";
            goodJson += '"goods_number":' + num;
            goodJson += "}";

        })
        goodJson += "]"
        api.openWin({
            name: 'orderConfirm_win',
            url: 'orderConfirm_win.html',
            bounces: false,
            pageParam: {
                orderType: "singleGood",
                goodOrderInfo: goodJson
            }
        });
    }
    function getCarList() {
        if(isLock) return;
        isLock=true;
        showLoading()
        var url = "cart/list";
        var data = {};
        data["page"]=page;
        getStorageAll();
//        alert(JSON.stringify(data))
        ajaxRequest(url, 'get', data, function (ret, err) {
            isLock=false;
            $(".contain").show();
            var content = $api.byId('carList-content');
            var tpl = $api.byId('carList-template').text;
            var tempFn = doT.template(tpl);
            if (page > 1) {
                if (!$.trim(ret.data)) {
                    isLock = true;
                }
                $api.append(content, tempFn(ret.data));
            } else {
                showNoTip(ret.data);
                $api.html(content, tempFn(ret.data));
            }

            hideLoading();
            mui('.mui-numbox').numbox();
            showTotal();
        });
    }
    function showTotal() {
        var total = 0;
        $("#carList-content").find("li").each(function () {
            var checkBox = $(this).find("input[type='checkbox']").eq(0).prop("checked")
            if (checkBox) {
                var count = $(this).find(".mui-numbox-input").eq(0).val();
                var price = $(this).find(".price").eq(0).text();
                total += count * price;
            }

        })
        $(".totalMoney").text(total)
    }
</script>
</html>